#include	<stdlib.h>
#include	<sys/types.h>
#include	<sys/stat.h>
#include	<fcntl.h>
#include	"my.h"

int		s_right(char **cmd, int oldfd)
{
  int		fd;

  fd = (xopen(cmd[0], O_CREAT | O_WRONLY | O_TRUNC));
  if (fd == -1)
    return (-1);
  if (xdup2(fd, oldfd) == -1)
    return (-1);
  return (1);
}

int		d_right(char **cmd, int oldfd)
{
  int		fd;

  fd = (xopen(cmd[0], O_CREAT | O_WRONLY | O_APPEND));
  if (fd == -1)
    return (-1);
  if (xdup2(fd, oldfd) == -1)
    return (-1);
  return (1);
}

char		**redir_right(int fd, char **cmd, t_redir *redir)
{
  if (redir->right == 1)
    {
      if (s_right(cmd, fd) == -1)
	return (NULL);
    }
  else if (redir->dright == 1)
    {
      if (d_right(cmd, fd) == -1)
	return (NULL);
    }
  return (cmd);
}
